package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.clsDictionary;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class ProveedoresPreferencialesDetalle extends Activity {

	public static ArrayList<Dictionary> Actividades = null;
	private String putCAT_CVE;
	private String putTITULO;
	public ListView list;
	public com.smartthinking.CICPNL.home.activity.ProveedoresPreferencialesDetalle.Hoteles_Adapter adapter;
	public Dialog progressDialog;
	public App_ImageLoader imageLoader;
	private Intent intent;
	private ArrayList<Dictionary> BannersHoteles = null;
	private ArrayList<Dictionary> ActividadesBanner;
	private String QRY_BUSCADOR;

	public void voidFinish(View V) {
		finish();

	}

	public void abrirMenuLateral(View V) {
		intent = new Intent(ProveedoresPreferencialesDetalle.this,
				MenuLateral.class);

		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_listview);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			try {

				this.putCAT_CVE = extras.getString("putCAT_CVE");
				this.putTITULO = extras.getString("putTITULO");

			} catch (Exception ex) {
			}

			// this.putTITULO = extras.getString("PUT_TITULO");

		}

		TextView Titulo = (TextView) this.findViewById(id.tv_listView_Titulo);
		Titulo.setText(putTITULO);

		// @+id/tv_listview_buscar_titutlo2

		// TextView TituloBuscar = (TextView) this
		// .findViewById(id.tv_listview_buscar_titutlo2);
		// TituloBuscar.setText(putTITULO);

		QRY_BUSCADOR = "select * from pois where cat_cve=" + putCAT_CVE
				+ " order by poi_idr";

		// @+id/tv_listview_buscar_titutlo2

		// TextView TituloBuscar = (TextView) this
		// .findViewById(id.tv_listview_buscar_titutlo2);
		// TituloBuscar.setText("Ponentes");

		final EditText buscar = (EditText) this
				.findViewById(id.et_buscar_buscar);

		ImageView buscador = (ImageView) this.findViewById(id.imageView1);
		buscador.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				QRY_BUSCADOR = "select * from pois where cat_cve=" + putCAT_CVE
						+ " and upper(a.poi_nombre) like '%"
						+ buscar.getText().toString().toUpperCase()
						+ "%' order by poi_idr";

				CargarContenido();
			}
		});

		CargarContenido();
		// abrirSplashBanner();
	}
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
		 
	}
	public void abrirSplashBanner() {
		/** se obtiene datos **/

		Intent intent = new Intent(this, Splash.class);
		intent.putExtra("PUT_IMG_WEB", "HOTELES");
		startActivity(intent);

	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	/**
	 * select * from banners WHERE BAN_TIPO IN ( 'BANNER HOTELES 1','BANNER
	 * HOTELES 2','BANNER HOTELES 3') ORDER BY ban_idr
	 * 
	 * 
	 * **/

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

	

		protected void onPostExecute(Integer in) {

			list = (ListView) findViewById(R.id.list);
			adapter = new Hoteles_Adapter(Actividades);
			list.setAdapter(adapter);

			list.setTextFilterEnabled(true);

			list.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					Intent intent = new Intent(
							ProveedoresPreferencialesDetalle.this,
							ProveedoresPreferencialesDetalle1.class);

					clsDictionary.setDctionary(Actividades.get(position));

					startActivity(intent);

					// finish();

				}
			});
			progressDialog.dismiss();

			// abrirSplashBanner();
		}

		protected void onPreExecute() {

			progressDialog = ProgressDialog.show(
					ProveedoresPreferencialesDetalle.this, "", "Cargando...");
		}

		protected Integer doInBackground(String... urls) {

			String qry_url = getString(R.string.QRY_URL)
					+ Uri.encode(QRY_BUSCADOR);

			saxXML saxparser = new saxXML(qry_url);

			Actividades = saxparser.parse();

			
			 

			return null;

		}

	}

	public class Hoteles_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Hoteles_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) ProveedoresPreferencialesDetalle.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			imageLoader = new App_ImageLoader(
					ProveedoresPreferencialesDetalle.this
							.getApplicationContext());

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {
			public TextView tvTitulo;
			public TextView tvSubtitulo;
			public ImageView icon;
			public ImageView vermas;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {

				vi = inflater.inflate(R.layout.ui_listview_items, null);

				holder = new ViewHolder();

				holder.tvTitulo = (TextView) vi
						.findViewById(R.id.tv_listview_items_titulo);

				holder.tvSubtitulo = (TextView) vi
						.findViewById(R.id.tv_listview_items_subtitulo);

				// @+id/iv_listview_items_imagen
				holder.icon = (ImageView) vi
						.findViewById(id.iv_listview_items_imagen);

				holder.vermas = (ImageView) vi.findViewById(id.iv_items_vermas);

				vi.setTag(holder);

			} else

				holder = (ViewHolder) vi.getTag();

			holder.icon.setVisibility(holder.icon.VISIBLE);

//			holder.vermas.setVisibility(holder.vermas.VISIBLE);

			String Nombre = data.get(position).get("poi_nombre").toString();
			holder.tvTitulo.setText(Nombre);

			String Direccion = data.get(position).get("poi_direccion")
					.toString();
			holder.tvSubtitulo.setText(Direccion);

			String url_img = getString(R.string.QRY_URL_IMG)
					+ data.get(position).get("poi_thumb").toString()
							.replace(" ", "%20");

			holder.icon.setTag(url_img);
			try {

				imageLoader.DisplayImage(url_img,
						ProveedoresPreferencialesDetalle.this, holder.icon);

			} catch (Exception ex) {

				String s = ex.getMessage().toString();

			}

			return vi;

		}

	}
}
